<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人" prop="empId">
        <!-- <el-input
          v-model="queryParams.empId"
          placeholder="请输入使用人"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->

<el-select v-model="queryParams.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>

      </el-form-item>

      <!-- <el-form-item label="归还人" prop="returnedBy">
        <el-input
          v-model="queryParams.returnedBy"
          placeholder="请输入归还人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      
      <!-- <el-form-item label="库存数量" prop="toolCount">
        <el-input
          v-model="queryParams.toolCount"
          placeholder="请输入库存数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="借用日期" prop="borrowimgDate">
        <el-date-picker clearable
          v-model="queryParams.borrowimgDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择借用日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="归还日期" prop="returnDate">
        <el-date-picker clearable
          v-model="queryParams.returnDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择归还日期">
        </el-date-picker>
      </el-form-item> -->
      <!-- 审核情况下拉框 -->
      <el-form-item label="审核情况" prop="audit">
        <!-- <el-input
          v-model="queryParams.audit"
          placeholder="请输入审核情况"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
         <el-select v-model="queryParams.audit" placeholder="请选择"
          clearable
          @keyup.enter.native="handleQuery">
            <el-option
              v-for="item in audits"
              :key="item.audit"
              :label="item.auditName"
              :value="item.audit">
            </el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!-- <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini" round
          @click="handleAdd"
          v-hasPermi="['tools:personal_tools:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini" round
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tools:personal_tools:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini" round
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tools:personal_tools:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini" round
          @click="handleExport"
          v-hasPermi="['tools:personal_tools:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personal_toolsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column  label="工具图片" align="center">
        <!--Vue插槽：（2.6.0以后版本弃用slot和slot-scope，改用v-slot)-->
        <template v-slot="{ row }">
          <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                    :src="getImage(row.img)"
                    >
          </el-image>
          <!-- :img01="getImage(row.img)" -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="编号" align="center" prop="toolId" /> -->
      <el-table-column label="使用人" align="center" prop="empName" />
      <!-- <el-table-column label="归还人" align="center" prop="returnedBy" /> -->

      <el-table-column label="归还人" align="center" prop="returnedBy">
        <template slot-scope="scope">

          
          
          {{scope.row.returnedBy==null?'暂无归还人':scope.row.returnedBy}}
        </template>
      </el-table-column>
    

      <el-table-column label="物品名称" align="center" prop="itemName" />
      <!-- <el-table-column label="库存数量" align="center" prop="toolCount" /> -->
      <!-- <el-table-column label="工具图片" align="center" prop="img" width="100">
        <template slot-scope="scope"  v-slot="{ row }">
          <image-preview  :width="50" :height="50" :src="getImage(row.img)"  style="width: auto; height: 40px; border:none;cursor: pointer;"/>
        </template>

      </el-table-column> -->
      <el-table-column label="借用日期" align="center" prop="borrowimgDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowimgDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归还日期" align="center" prop="returnDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
          {{scope.row.returnDate==null?'暂无归还日期':''}}
        </template>
      </el-table-column>
      <el-table-column label="审核情况" align="center" prop="audit">
        <template slot-scope="scope">
          <el-tag  type="warning"  v-if="scope.row.audit==0">审核中</el-tag>
          <el-tag type="success" v-if="scope.row.audit==1">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.audit==2">未通过</el-tag>
          <!-- {{scope.row.audit==0?'审核中':''}}
          {{scope.row.audit==1?'通过':''}}
          {{scope.row.audit==2?'未通过':''}} -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tools:personal_tools:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tools:personal_tools:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人工具管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-show="title == '修改个人工具管理'" label="回显图片" align="center">
          <!--Vue插槽：（2.6.0以后版本弃用slot和slot-scope，改用v-slot)-->
          <template>
            <el-image style="width: auto; height: 90px; border:none;cursor: pointer;"
                      :src="getImage(form.img)">
            </el-image>
          </template>
        </el-form-item>
        <el-form-item label="工具头像" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            drag
            action="http://localhost:8080/image/upload/"
            :on-success="handle_success"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将图片拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="使用人" prop="empId">
          <el-input v-model="form.empId" placeholder="请输入使用人" />
        </el-form-item> -->
<!--        员工编号改下拉框-->
        <el-form-item label="员工姓名" prop="empId"  >
<!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId"  :disabled="verification1" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId"
             >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核情况" prop="audit">
          <!-- <el-input v-model="form.audit" placeholder="请输入审核情况" /> -->
          <el-select v-model="form.audit" placeholder="请选择">
            <!--          clearable-->
            <!--          @keyup.enter.native="handleQuery">-->
            <el-option
              v-for="item in audits"
              :key="item.audit"
              :label="item.auditName"
              :value="item.audit">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品名称" prop="itemName">
          <el-input v-model="form.itemName" :disabled="verification1" placeholder="请输入物品名称" />
        </el-form-item>
        <!-- <el-form-item label="库存数量" prop="toolCount">
          <el-input v-model.number="form.toolCount" placeholder="请输入库存数量" />
        </el-form-item> -->
        <el-form-item label="借用日期" prop="borrowimgDate">
          <el-date-picker clearable
            v-model="form.borrowimgDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归还人" prop="returnedBy">
          <el-input v-model="form.returnedBy" placeholder="请输入归还人" :disabled="verification" />
        </el-form-item>
        <el-form-item label="归还日期" prop="returnDate">
          <el-date-picker clearable
            v-model="form.returnDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择归还日期"
            :disabled="verification"
            >
          </el-date-picker>
        </el-form-item>

<!--        <el-form-item label="审核情况" prop="audit">-->
          <!-- <el-input v-model="form.audit" placeholder="请输入审核情况" /> -->
<!--           <el-select v-model="form.audit" placeholder="请选择">-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery">-->
<!--            <el-option-->
<!--              v-for="item in audits"-->
<!--              :key="item.audit"-->
<!--              :label="item.auditName"-->
<!--              :value="item.audit">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <!-- <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPersonal_tools,
  getPersonal_tools,
  delPersonal_tools,
  addPersonal_tools,
  updatePersonal_tools
} from "@/api/tools/personal_tools";
import { listEmployee1 } from "@/api/personnel/employee";
import { log } from 'util';

export default {
  verification1:true,
  verification: true,
  name: "Personal_tools",
  data() {
    return {
      formLabelWidth: '80px',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人工具管理表格数据
      personal_toolsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        empId: null,
        returnedBy: null,
        itemName: null,
        toolCount: null,
        borrowimgDate: null,
        returnDate: null,
        audit: null,
        version: null,
        img: null
      },
      audits: [
        { audit: 0, auditName: "审核中" },
        { audit: 1, auditName: "通过" },
        { audit: 2, auditName: "未通过" }
      ],
      employee: [],
      // 表单参数
      form: {},
      //大图预览
      // img01:"",
      //srcList:['https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg'],
      //  srcList:[],
       //http://localhost:8080/image/download?name=470b6c2651d546cfb548746aa5db6cba.png


      // 表单校验
      rules: {
        empId: [{ required: true, message: "员工姓名不能为空", trigger: "blur" }],
        itemName: [
          { required: true, message: "物品名称不能为空", trigger: "blur" }
        ],
        toolCount: [
           { type: "number", message: "库存数量必须为数字" ,required: true},
        ],
        audit : [
          { required: true, message: "审核情况不能为空", trigger: "blur" }
        ],
        // returnedBy: [
        //   { required: true, message: "归还人不能为空", trigger: "blur" },
        //   {
        //     validator: function(rule, value, callback) {
        //       //校验中文的正则：/^[\u4e00-\u9fa5]{0,}$/
        //       // ^[a-zA-Z\u4e00-\u9fa5]+$
        //       if (/^[a-zA-Z\u4e00-\u9fa5]+$/.test(value) == false) {
        //         callback(new Error("请输入中文或英文"));
        //       } else {
        //         //校验通过
        //         callback();
        //       }
        //     },
        //     trigger: "change"
        //   }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // 发送请求
    listEmployee1().then(response => {
      this.employee = response.rows;
      this.loading = false;
    });
  },
  methods: {
    // getSrcList(image){

    //     this.srcList[0] = 'http://localhost:8080/image/download?name='+image
    // },
     // 图片加载到本地获取到返回的名称给到from表单数据
    handle_success(res){
        this.form.img = res
    },
     getImage(image){
      // console.log(image)
      return 'http://localhost:8080/image/download?name='+image
    },
    /** 查询个人工具管理列表 */
    getList() {
      this.loading = true;
      listPersonal_tools(this.queryParams).then(response => {
        this.personal_toolsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        toolId: null,
        empId: null,
        returnedBy: null,
        itemName: null,
        toolCount: null,
        borrowimgDate: null,
        returnDate: null,
        audit: null,
        version: null,
        img: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.toolId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.verification1 = false;
      this.verification = true;
      this.reset();
      this.open = true;
      this.title = "添加个人工具管理";
      // 发送请求
      // listEmployee1().then(response => {
      //   this.employee = response.rows;
      //   this.loading = false;
      // });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.verification1 = true;
      this.verification = false;
      this.reset();
      const toolId = row.toolId || this.ids;
      getPersonal_tools(toolId).then(response => {
        this.form = response.data;
        this.img01 = response.data.img
        console.log("111111"+response.data.img);
        console.log(this.img01);


        this.open = true;
        this.title = "修改个人工具管理";
      });
      // 发送请求
      // listEmployee1().then(response => {
      //   this.employee = response.rows;
      //   this.loading = false;
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.toolId != null) {
            updatePersonal_tools(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonal_tools(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const toolIds = row.toolId || this.ids;
      this.$modal
        .confirm('是否确认删除个人工具管理编号为"' + toolIds + '"的数据项？')
        .then(function() {
          return delPersonal_tools(toolIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tools/personal_tools/export",
        {
          ...this.queryParams
        },
        `personal_tools_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
